package com.itstudy.xxx.java8.other;

import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author xianpengcheng@aliyun.com
 * @create 2019-05-12 15:43
 */
public class StreamPractice {

    List<Transaction> transactions = null;

    @Before
    public void before(){
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );
    }

    List<Cust> custs = Arrays.asList(
            new Cust(101,"梅西",30,33000000L),
            new Cust(102,"伊布",35,23000000L),
            new Cust(103,"哈维",34,20000000L),
            new Cust(104,"伊列斯塔",33,18000000L),
            new Cust(105,"小罗",37,15000000L),
            new Cust(106,"内马尔",27,32000000L),
            new Cust(106,"内马尔",27,32000000L),
            new Cust(106,"姆巴佩",23,30500000L)
    );


    @Test
    public void test0() {
        //对数组每个数字平方后输出
        List<Integer> collect = Arrays.asList(1, 2, 3, 4).stream().map(x -> x * x).collect(Collectors.toList());
        for (Integer c :collect){
            System.out.println(c);
        }
        Integer reduce = custs.stream().map(e -> 1).reduce(0, Integer::sum);
        System.out.println(reduce);
        //1. 找出2011年发生的所有交易， 并按交易额排序（从低到高）
        List<Transaction> collect1 = transactions.stream().filter(e -> e.getYear() == 2011).sorted((x, y) -> {
            if (x.getValue() > y.getValue()) {
                return -1;
            } else if (x.getValue() < y.getValue()) {
                return 1;
            } else {
                return 0;
            }
        }).collect(Collectors.toList());
        System.out.println(collect1);
//        交易员都在哪些不同的城市工作过？
        List<String> collect2 = transactions.stream().map(Transaction::getTrader).map(Trader::getCity).distinct().collect(Collectors.toList());
        System.out.println(collect2);
        List<String> collect3 = transactions.stream().map(e -> e.getTrader().getCity()).distinct().collect(Collectors.toList());
        System.out.println(collect3);
//3. 查找所有来自剑桥的交易员，并按姓名排序

        List<Transaction> cambridge = transactions.stream().filter(e -> e.getTrader().getCity().equals("Cambridge")).sorted((x, y) -> {
            return x.getTrader().getName().compareTo(y.getTrader().getName());
        }).collect(Collectors.toList());
        System.out.println(cambridge);

        //4. 返回所有交易员的姓名字符串，按字母顺序排序
        String collect4 = transactions.stream().map(e -> e.getTrader().getName()).sorted().distinct().collect(Collectors.joining("|"));
        System.out.println(collect4);
        //5. 有没有交易员是在米兰工作的？
        boolean milan = transactions.stream().anyMatch(e -> e.getTrader().getCity().equals("milan"));
        System.out.println(milan);
//6. 打印生活在剑桥的交易员的所有交易额
        Map<String, Integer> cambridge1 = transactions.stream().filter(e -> e.getTrader().getCity().equals("Cambridge")).collect(Collectors.groupingBy(e -> e.getTrader().getName(), Collectors.summingInt(e -> e.getValue())));
        System.out.println(cambridge1);
//7. 所有交易中，最高的交易额是多少
        Optional<Integer> collect5 = transactions.stream().map(e -> e.getValue()).collect(Collectors.maxBy(Integer::compare));
        System.out.println(collect5.get());
        //8. 找到交易额最小的交易
        Optional<Transaction> min = transactions.stream().min((x, y) -> Integer.compare(x.getValue(), y.getValue()));
        System.out.println(min);


    }















}

